package com.app.utils.jst;

import com.alibaba.fastjson.JSONObject;
import com.app.utils.base.http.McodeHandler;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.DateUtil;
import com.app.utils.base.j2se.StringHandler;
import com.app.utils.jst.paramBean.OrderParam;
import com.app.web.jst.Order;
import com.jushuitan.api.*;
import com.jushuitan.api.exception.ApiException;

import java.util.Map;

public class JstHandler {

    private static final ApiClient apiClient = new DefaultApiClient();

    //access_token=bee6f26d8b4c42179160c0a864046995;
    //refresh_token=f7216386b13a401daf435b6e92cd37f4;
    //expires_in=31104000

    // 替换为POP分配给应用的app_key,app_secret,accessToken等
    // 0d4986b7dd1340cfa48bce39516baa70
    private static final String appKey = "0d4986b7dd1340cfa48bce39516baa70";
    // aa510a1c876d4336b6d98d8af5a3eda9
    private static final String appSecret = "aa510a1c876d4336b6d98d8af5a3eda9";
    //bee6f26d8b4c42179160c0a864046995
    private static final String accessToken = "bee6f26d8b4c42179160c0a864046995";

    public static Map<String,Object> execute(String url, String biz){
        // 设置超时时间
//        ((DefaultApiClient)client).setReadTimeout(3000);
//        ((DefaultApiClient)client).setConnectTimeout(2000);
        // 构建请求对象
        ApiRequest request = new ApiRequest.Builder(url, appKey, appSecret)
                .biz(biz).build();

        // 执行接口调用
        try {
            ApiResponse response = apiClient.execute(request, accessToken);
            if(response.isSuccess()){
                JSONObject map = JSONObject.parseObject(response.getBody());
                if(Convert.objToInt(map.get("code"))==0){
                    return map.getInnerMap();
                }
            }
        } catch (ApiException e) {
//            switch (e.getCodeEnum()) {
//                case SYS_ERROR:
//                    // TODO retry
//                    break;
//                case PARAM_ERROR:
//                    // TODO check param
//                    break;
//                case SIGN_ERROR:
//                    // TODO @see com.jushuitan.api.util.ApiUtil
//                    break;
//                case CONNECT_ERROR:
//                    // TODO check network
//                    break;
//                case TIME_OUT_ERROR:
//                    // TODO check logic or reset readTimeout
//                    break;
//                case EXECUTE_ERROR:
//                    // TODO @see com.jushuitan.api.DefaultApiClient#executeRemote
//                    break;
//                default:
//                    // log
//                    break;
//            }
            e.printStackTrace();
//            Logger.error("聚水潭获取商品类目api失败",e.getCause());
        } catch (Exception e) {
            e.printStackTrace();
//            Logger.error("聚水潭获取商品类目错误",e.getCause());
        }
        return null;
    }

    public static OrderParam param(Map<String,Object> order,Map<String,Object> addrInfo){
        JSONObject unit = JSONObject.parseObject(Convert.objToStr(order.get("goods_units")));
        OrderParam param = OrderParam.builder().shop_id(15373400)
                .so_id(order.get("order_sn")+"")
                .order_date(DateUtil.getDatetime())
                .shop_status("WAIT_SELLER_SEND_GOODS")
                .shop_buyer_id(McodeHandler.to32Radix(Convert.objToLong(order.get("uid"))))
                .receiver_state(StringHandler.getString(addrInfo.get("province")))
                .receiver_city(StringHandler.getString(addrInfo.get("city")))
                .receiver_district(StringHandler.getString(addrInfo.get("county")))
                .receiver_address(StringHandler.getString(addrInfo.get("addrInfo")))
                .receiver_name(StringHandler.getString(addrInfo.get("name")))
                .receiver_phone(StringHandler.getString(addrInfo.get("phone")))
                .pay_amount(Convert.strToFloat(StringHandler.fenToYuan(order.get("order_amount")+""), 0.00F))
                .freight(Convert.strToFloat("4.0", 0.00F))
                .item(OrderParam.Item.builder()
                        .sku_id(unit.get("item_code")+"")
                        .shop_sku_id(unit.get("item_code")+"")
                        .amount(0.00F)
                        .base_price(0.00F)
                        .qty(Convert.objToInt(order.get("goods_num")))
                        .name(order.get("goods_name")+"")
                        .outer_oi_id(order.get("order_sn")+"")
                        .build())
                .pay(OrderParam.Pay.builder().outer_pay_id(order.get("order_sn")+"")
                        .pay_date(DateUtil.getDatetime())
                        .payment("其他")
                        .seller_account("1")
                        .buyer_account("2")
                        .amount(Convert.strToFloat(StringHandler.fenToYuan(order.get("order_amount")+""), 0.00F))
                        .build())
                .build();
        return param;
    }
}
